package com.project.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lyl
 * description:登录过滤器
 */
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //从session中取出登录用户
        Object obj = request.getSession().getAttribute("loginUser");
        if (obj != null) {
            //如果合法，则允许让请求继续访问目标资源
            filterChain.doFilter(request, response);
        } else {
            //如果用户没有登录则跳转登录页面
            response.sendRedirect("/html/login.html?info=noland");
        }
    }
}
